package com.gw2.gw2world.controller;

import com.gw2.gw2world.ActiveEventActivity;
import com.gw2.gw2world.R;
import com.gw2.gw2world.SettingsActivity;
import com.gw2.gw2world.TempleDungeonMenuActivity;
import com.gw2.gw2world.WorldEventActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class NavigationManager implements ListView.OnItemClickListener {
	@SuppressWarnings("unused")
	private static final String TAG = "NavigationManager";
	private Activity owner;
	private Intent intent;
	private DrawerLayout mDrawerLayout;
	private ActionBarDrawerToggle mDrawerToggle;
    private ListView mDrawerList;
    private ItemAdapter adapter;
    private Integer[] items=new Integer[] {R.string.world_events,R.string.all_events,R.string.temple_dungeon,R.string.action_settings};
    private Integer[] icons=new Integer[] {R.drawable.ic_chest2,R.drawable.ic_fight,R.drawable.ic_temple,R.drawable.ic_config};
    @SuppressWarnings("rawtypes")
	private Class[] actions=new Class[]{WorldEventActivity.class, ActiveEventActivity.class, TempleDungeonMenuActivity.class, SettingsActivity.class};
	public NavigationManager(Activity a, int layout_id, int listId) {
		owner=a;
		adapter=new ItemAdapter(owner, items);
		mDrawerLayout = (DrawerLayout) owner.findViewById(layout_id);
        mDrawerList = (ListView) owner.findViewById(listId);
        mDrawerList.setAdapter(adapter);
        mDrawerList.setOnItemClickListener(this);
        mDrawerToggle = new ActionBarDrawerToggle(
                owner,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_navigation_drawer,  // nav drawer image to replace 'Up' caret. Get it here: http://android-ui-utils.googlecode.com/hg/asset-studio/dist/icons-nav-drawer-indicator.html
                R.string.open,  /* "open drawer" description for accessibility */
                R.string.close  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                //owner.invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
            	//owner.invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        owner.getActionBar().setDisplayHomeAsUpEnabled(true);
        owner.getActionBar().setHomeButtonEnabled(true);
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if(owner.getClass()!=actions[position]) {
			intent=new Intent(owner, actions[position]);
			owner.startActivity(intent);
		}
		//mDrawerList.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(mDrawerList);
	}
	private class ItemAdapter extends ArrayAdapter<Integer> {
		public ItemAdapter(Context context, Integer[] i) {
            super(context, R.layout.drawer_list_item, i);
            items = i;
        }
		public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)owner.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.drawer_list_item, null);
                TextView tv=(TextView)v.findViewById(R.id.tv_drawer_item);
                tv.setText(items[position]);
                tv.setCompoundDrawablesWithIntrinsicBounds(icons[position], 0, 0, 0);
            }
            return v;
		}
	}
	public DrawerLayout getDrawerLayout() {return mDrawerLayout;}
	public boolean isDrawerOpen() {return mDrawerLayout.isDrawerOpen(mDrawerList);}
	public ActionBarDrawerToggle getDrawerToggle() {return mDrawerToggle;}
}
